//
//  LandController2.m
//  quickLook
//
//  Created by J on 15/9/3.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "LandController2.h"

#import "Header.h"
#import "LandController3.h"
@interface LandController2 ()
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;

@property(nonatomic,strong)UIAlertView* alertView;

@end

@implementation LandController2

- (void)viewDidLoad{

    [super viewDidLoad];
    
    _registerButton.layer.cornerRadius=3;

}
- (IBAction)registerButton:(id)sender {
    
//    http://api.kuaikanmanhua.com/v1/phone/send_code
    
    
//    18518475047
    

    JZJLog(@"%@",_phoneNumber.text);
    
    if ([self validateMobile:_phoneNumber.text]) {
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        NSDictionary *params = @{@"reason":@"register", @"phone":_phoneNumber.text};
//        JZJLog(@"%@",_phoneNumber);
        
        [manager POST:@"http://api.kuaikanmanhua.com/v1/phone/send_code" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //如果返回已经注册 那么显示已经注册
            
            if ([responseObject[@"message"]isEqualToString:@"exist phone number"]) {
                
                
                [_alertView show];

                
            }
            else{
            
            
                LandController3 * land3=[[LandController3 alloc]initWithNibName:@"LandController3" bundle:nil];
                
                land3.infoLabel.text=[NSString stringWithFormat:@"已发送验证码短信至%@",_phoneNumber.text];
                
                land3.phoneNumber=_phoneNumber.text;
                
                
                
                JZJLog(@"infoLabel::%@",land3.infoLabel.text);
                
                
                
                land3.hidesBottomBarWhenPushed=YES;
                
                
                [self.navigationController pushViewController:land3 animated:YES];
                
                

                
                
            
            }
            JZJLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            JZJLog(@"Error: %@", error);
        }];
        
        
        
        
        
        
        
        

        
        
    }
    
    else{
    
    
        _alertView = [[UIAlertView alloc] initWithTitle:nil
                      
                      
                      
                
                                                                         message:@"您输入的手机号无效! 请重新输入"
                                                                        delegate:nil
                                                               cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        
                      
       [NSTimer scheduledTimerWithTimeInterval:0.5f
                                                                   target:self
                                                                 selector:@selector(dismissAlertView:)
                                                                 userInfo:nil
                                                                  repeats:NO];
                                 
                                  [_alertView show];
  
    
    }
    

    
}


- (void)dismissAlertView:(NSTimer*)timer {
    
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}




- (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
